package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.DepartmentService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo) {
        //判断当前登录用户是否有该角色
        boolean hr_msg = SecurityUtils.getSubject().hasRole("HR_MGR");
        System.out.println(hr_msg);

        PageInfo<Department> pageResult = departmentService.query(qo);

        model.addAttribute("pageResult", pageResult);

        return "department/list";
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id) {

        try{
            if (id != null){
                departmentService.delete(id);
            }
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"失败");
        }
    }

/*
    @RequestMapping("/input")
    public String input(Long id,Model model) {
        if (id != null){
            Department department = departmentService.selectById(id);
            model.addAttribute("department",department);
        }

        return "department/input";
    }
*/

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Department department) {

        try{
            if (department.getId() != null){
                departmentService.update(department);
            }else {
                departmentService.save(department);
            }
            return new JsonResult();
        }catch(Exception e){
            e.printStackTrace();
            return new JsonResult(false,"操作失败");
        }
    }
}
